﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class QuarterPlan_FishBoneDiagramList : System.Web.UI.Page
{
    Model.UserInfo objUserInfo = new Model.UserInfo();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDrp();
            BindGdv();
        }
    }

    /// <summary>
    /// 绑定鱼骨图
    /// </summary>
    void BindGdv()
    {
        object[] _value = new object[3];
        _value[0] = this.drpYear.SelectedValue;
        _value[1] = Convert.ToInt32(this.drpDepartment.SelectedValue);
        _value[2] = 1;
        DataTable dt = new BLL.FishBoneDiagram().GetFishBoneByDepartmentAndYear(_value);

        this.gdvFishBoneDiagram.DataSource = dt;
        this.gdvFishBoneDiagram.DataBind();
        for (int i = 0; i < this.gdvFishBoneDiagram.Rows.Count; i++)
        {
            string UploadUrl = this.gdvFishBoneDiagram.DataKeys[i]["UploadUrl"].ToString();
            Button btn = (Button)this.gdvFishBoneDiagram.Rows[i].FindControl("btnView");
            if (UploadUrl.Length > 0)
            {
                btn.Visible = true;
            }
            else
            {
                btn.Visible = false;
            }
        }
    }



    /// <summary>
    /// 绑定下拉信息
    /// </summary>
    void BindDrp()
    {
        for (int i = 2009; i < 2100; i++)
        {
            ListItem li = new ListItem(i.ToString(), i.ToString());
            this.drpYear.Items.Add(li);
        }
        DataTable _year = new BLL.QuarterDateManager().getQuarterDate();
        this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));


        //if (objUserInfo.UserName != "admin")
        //{
        //    object[] _value = new object[1];
        //    _value[0] = objUserInfo.UserName;
        //    DataTable _userTable = new BLL.UserInfoManager().get(BLL.UserInfoManager.field.UserName.ToString(), _value);
        //    if (_userTable.Rows.Count > 0)
        //    {
        //        if (_userTable.Rows[0]["DepartmentId"].ToString().Length > 0)
        //        {
        //            ListItem _li = new ListItem(_userTable.Rows[0]["DepartmentId"].ToString(), _userTable.Rows[0]["DId"].ToString());
        //            this.drpDepartment.Items.Add(_li);
        //        }
        //        if (_userTable.Rows[0]["PartTimeDepartmentId"].ToString().Length > 0)
        //        {
        //            ListItem _li = new ListItem(_userTable.Rows[0]["PartTimeDepartmentId"].ToString(), _userTable.Rows[0]["PDid"].ToString());
        //            this.drpDepartment.Items.Add(_li);
        //        }
        //    } 
        //}
        //else
        //{
            this.drpDepartment.DataSource = new BLL.DepartmentInfoManager().getNoTree();
            this.drpDepartment.DataTextField = "DepartmentName";
            this.drpDepartment.DataValueField = "DepartmentId";
            this.drpDepartment.DataBind();
            ListItem _li = new ListItem("请选择部门", "0");
            this.drpDepartment.Items.Insert(0, _li);
            object[] _value = new object[1];
            _value[0] = objUserInfo.UserName;
            DataTable _userTable = new BLL.UserInfoManager().get(BLL.UserInfoManager.field.UserName.ToString(), _value);
            if (_userTable.Rows.Count > 0)
            {
                this.drpDepartment.SelectedIndex = this.drpDepartment.Items.IndexOf(this.drpDepartment.Items.FindByText(_userTable.Rows[0]["DepartmentId"].ToString()));
            }
        //}
    }

    /// <summary>
    /// 查看鱼骨图
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnView_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引

        //string Id = this.gdvFishBoneDiagram.DataKeys[_rowIndex]["Id"].ToString();
        //string _url = "DisplayAttachment.aspx?Id=" + Id + "&TypeId=0";
        //Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=800,width=1000,top=300,left=250,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no');</script>");

 
        //读取隐藏地址列
        string path = this.gdvFishBoneDiagram.DataKeys[_rowIndex]["UploadUrl"].ToString();
        // Response.Redirect("~/upload/"+path);
        FileDownload(path); 
    }


    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {

        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }

    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGdv();
    }
    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGdv();
    }
}